package com.offcn.sellergood.controller;

import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.sellergood.pojo.Item;
import com.offcn.sellergood.service.ItemService;
import com.offcn.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:ujiuye
 * @Description:
 * @Date 2021/2/1 14:19
 *****/
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private TokenDecode tokenDecode;

    @PostMapping("/decr/count")
    public Result decr(){
        String username = tokenDecode.getUsername();
        try {
            itemService.dearCount(username);
            return new Result(true,StatusCode.OK,"减库存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"减库存失败");
        }
    }

    @GetMapping("/status/{status}")
    public Result<List<Item>> findByStatus(@PathVariable("status") String status){
        try {
            List<Item> items = itemService.findByStatus(status);
            return new Result<>(true, StatusCode.OK,"查询成功",items);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(true, StatusCode.OK,"查询成功",null);
        }
    }

    // 根据商品的id来获取商品的信息
    @GetMapping("/{id}")
    public Result<Item> findById(@PathVariable Long id){
        Item item = itemService.findById(id);
        return new Result<>(true,StatusCode.OK,"查询成功",item);
    }
}
